<!--detail-->
<table>
    <tbody id="salesOrderDetailFooter">
    	<tr>
    		<td>
    			<label for="searchProductNumber"><g:message code="b2b.salesOrderDetail.search.product" default="Search Product" />:</label>
		        <g:textField name="searchProductNumber" style="width:300px;"/>
		        <g:hiddenField name="searchProductId"/>
		        <span id="searchProductName"></span>
		        ,<span id="searchProductStandard"></span>
		        ,<span id="searchProductPrice"></span>
			</td>
    	</tr>
    </tbody>
</table>
<table>
    <thead>
        <tr>
            <th><g:message code="b2b.salesOrderDetail.serialNumber" default="Serial Number" /></th>
            <th><g:message code="b2b.salesOrderDetail.product" default="Product" /></th>
            <th><g:message code="b2b.product.name" default="Product Name" /></th>
            <th><g:message code="b2b.product.standard" default="Standard" /></th>
            <th><g:message code="b2b.product.category" default="Category" /></th>
            <th>
                <g:message code="b2b.salesOrderDetail.price" default="Price" />
                (<g:message code="b2b.category.isAllowZeroPrice" default="Allow Zero Price?" />)
            </th>
            <th><g:message code="b2b.salesOrderDetail.discount" default="Discount" /></th>
            <th>
                <g:message code="b2b.salesOrderDetail.quantity" default="Order Quantity" />
            </th>
            <th>
                <g:message code="b2b.salesOrderDetail.deliveryLimitation" default="Delivery Limitation" />
                (<g:message code="b2b.category.deliveryCycle" default="Delivery Cycle" />)
            </th>
            <th><g:message code="b2b.salesOrderDetail.amount" default="Amount" /></th>
            <th><g:message code="b2b.salesOrderDetail.contractDetail" default="Contract Detail" /></th>
            <th>
                <g:message code="b2b.salesOrderDetail.specialDiscount" default="Special Discount" />
                (<g:message code="b2b.category.isAllowSpecialDiscount" default="Allow Special Discount?" />)
            </th>
            <th><g:message code="b2b.salesOrderDetail.specialAmount" default="Special Amount" /></th>
            <th><g:message code="b2b.salesOrderDetail.finalDiscount" default="Final Discount" /></th>
            <th><g:message code="b2b.salesOrderDetail.finalAmount" default="Final Amount" /></th>
        </tr>
    </thead>
    <tbody id="salesOrderDetailList">
	<!--define total amount-->
    <g:set var="_total_amount" value="${0}"/>
	<!--define total special amount-->
    <g:set var="_total_special_amount" value="${0}" />
	<!--define total final amount-->
    <g:set var="_total_final_amount" value="${0}" />
	<!--begin salesOrderDetail each-->
    <g:each in="${salesOrderInstance?.salesOrderDetails}" status="i" var="salesOrderDetailInstance">
		<!--calc row amount-->
		<g:set var="_amount"
			value="${(salesOrderDetailInstance?.amount)?:((salesOrderDetailInstance?.price ?:0) * (salesOrderDetailInstance?.discount ?: 0) * (salesOrderDetailInstance?.quantity ?: 0))}" />       	
		<!-- row special amount -->
		<g:set var="_special_amount" 
			value="${(salesOrderDetailInstance?.specialAmount)?:((salesOrderDetailInstance?.price ?: 0) * (salesOrderDetailInstance?.discount ?: 0) * (salesOrderDetailInstance?.specialDiscount ?: 0) * (salesOrderDetailInstance?.quantity ?: 0))}" />       	
		<!-- row final amount -->
		<g:set var="_final_amount" 
			value="${(salesOrderDetailInstance?.finalAmount)?:((salesOrderDetailInstance?.price ?: 0) * (salesOrderDetailInstance?.discount ?: 0) * (1 - (salesOrderDetailInstance?.specialDiscount ?: 0)) * (salesOrderDetailInstance?.quantity ?: 0))}" />       	
		<!-- total amount plus -->
		<g:set var="_total_amount" value="${(_total_amount ?: 0) + (_amount ?: 0)}"></g:set>
		<!-- total special amount plus -->
		<g:set var="_total_special_amount" value="${(_total_special_amount ?: 0) + (_special_amount ?: 0)}"></g:set>
		<!-- total final amount plus -->
		<g:set var="_total_final_amount" value="${(_total_final_amount ?: 0) + (_final_amount ?: 0)}"></g:set>
		<tr class="${(i % 2) == 0 ? 'odd' : 'even'}" >
			<!-- serial number -->
			<td name="serialNumber">
		    	<span>${fieldValue(bean: salesOrderDetailInstance, field: "serialNumber")}</span>
		    	<g:hiddenField name="salesOrderDetails[${i}].serialNumber" value="${salesOrderDetailInstance?.serialNumber}"/>
		    	<br/>
		    	<a href="#addSalesOrderDetailLink" delete="delete">${message(code: 'default.button.delete', default: 'Delete')}</a>
		    </td>
		    <!-- product.id -->
		    <td name="product.id">
		    	${fieldValue(bean: salesOrderDetailInstance, field: "product")}
		    	<g:hiddenField name="salesOrderDetails[${i}].product.id" value="${salesOrderDetailInstance?.product?.id}"/>
		    </td>
		    <!-- product.name -->
		    <td name="product.name">${fieldValue(bean: salesOrderDetailInstance, field: "product.name")}</td>
		    <!-- product.standard -->
		    <td name="product.standard">${fieldValue(bean: salesOrderDetailInstance, field: "product.standard")}</td>
		    <!-- category -->
		    <td name="category">
		    	${include(controller : 'salesOrderAjax', action : 'productCategoryOfCG', params : [product : salesOrderDetailInstance?.product?.serialNumber] )}
		    </td>
		    <!-- price -->
		    <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.price', 'errors')} number" name="price">
		    	<g:textField name="salesOrderDetails[${i}].price" value="${formatNumber( number : salesOrderDetailInstance?.price, format:'###,##0.000000')}" readonly="readonly"/>
                <span>
            		(<g:include controller="salesOrderAjax" action="isAllowZeroPrice" params="[product:salesOrderDetailInstance?.product?.serialNumber]"/>)
            	</span>
		    </td>
		    <!-- discount -->
		    <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.discount', 'errors')} number" name="discount">
		    	<g:textField name="salesOrderDetails[${i}].discount" value="${formatNumber( number : salesOrderDetailInstance?.discount, format:'###,##0.0000')}" readonly="readonly"/>
		    </td>
		    <!-- quantity -->
		    <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.quantity', 'errors')} number" name="quantity" >
		    	<g:textField name="salesOrderDetails[${i}].quantity" value="${formatNumber( number : salesOrderDetailInstance?.quantity, format:'###,##0.00')}"/>
		    </td>
		    <!-- delivery limitation -->
		    <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.deliveryLimitation', 'errors')}" name="deliveryLimitation">
		    	<g:jqDatePicker name="salesOrderDetails[${i}].deliveryLimitation" value="${salesOrderDetailInstance?.deliveryLimitation}"/>
		    	<span>
		    		<g:include controller="salesOrderAjax" action="deliveryCycle" params="[product:salesOrderDetailInstance?.product?.serialNumber]"/>
		    	</span>
			</td>
			<!-- amount -->
		    <td class="value number" name="amount" >
		        <g:formatNumber number="${_amount}" format="###,##0.0000" />
		    </td>
		    <!-- contract detail -->
		    <td name="contractDetail.id">
		    	<span>
					<g:link controller="contract" action="show" 
                        id="${include(controller : 'salesOrderAjax', action : 'getContractId', params : [contractDetailId : salesOrderDetailInstance?.contractDetail?.id] )}">
						<g:include 
                            controller = 'salesOrderAjax'
                            action = 'getContractDetailSerialNumber'
                            params = "[contractDetailId : salesOrderDetailInstance?.contractDetail?.id]"
                        />
					</g:link>
				</span>
		    	<g:hiddenField name="salesOrderDetails[${i}].contractDetail.id" value="${salesOrderDetailInstance?.contractDetail?.id}"/>
		    </td>
		    <!-- special discount -->
		    <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.specialDiscount', 'errors')} number" name="specialDiscount">
		    	<g:textField 
					name="salesOrderDetails[${i}].specialDiscount" 
					value="${formatNumber( number : salesOrderDetailInstance?.specialDiscount, format:'###,##0.0000')}" 
					readonly="readonly"/>
                <span>
            		(<g:include controller="salesOrderAjax" action="isAllowSpecialDiscount" params="[product:salesOrderDetailInstance?.product?.serialNumber]"/>)
            	</span>
		    </td>
		    <!-- special amount -->
		    <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.specialAmount', 'errors')} number" name="specialAmount">
		        <g:formatNumber number="${_special_amount}" format="###,##0.0000" />
		    </td>
		    <!-- final discount -->
		    <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.finalDiscount', 'errors')} number" name="finalDiscount">
		    	<g:textField 
                    name="salesOrderDetails[${i}].finalDiscount" 
                    value="${formatNumber( number : salesOrderDetailInstance?.finalDiscount, format:'###,##0.0000')}" 
                    readonly="readonly"/>
		    </td>
		    <!-- final amount -->
		    <td class="value ${hasErrors(bean: salesOrderInstance, field: 'salesOrderDetails.finalAmount', 'errors')} number" name="finalAmount">
		        <g:formatNumber number="${_final_amount}" format="###,##0.0000" />
		    </td>
		</tr>
    </g:each>
    </tbody>
    <tfoot>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>
            	<label for="totalAmount"><g:message code="b2b.salesOrder.salesOrderDetails.amount.sum" default="Total Amount" /></label>
            </td>
            <td name="totalAmount">
            	<g:formatNumber number="${_total_amount?:0}" format="###,##0.0000" />
            </td>
            <td>&nbsp;</td>
            <td>
            	<label for="totalSpecialAmount"><g:message code="b2b.salesOrder.salesOrderDetails.specialAmount.sum" default="Total Special Amount" /></label>
            </td>
            <td name="totalSpecialAmount">
            	<g:formatNumber number="${_total_special_amount?:0}" format="###,##0.0000" />
            </td>
            <td>
            	<label for="totalFinalAmount"><g:message code="b2b.salesOrder.salesOrderDetails.finalAmount.sum" default="Total Final Amount" /></label>
            </td>
            <td name="totalFinalAmount">
            	<g:formatNumber number="${_total_final_amount?:0}" format="###,##0.0000" />
            </td>
        </tr>
    </tfoot>
</table>
<!--end detail-->
